package com.shop.impl;

import com.alibaba.fastjson.JSONObject;
import com.shop.api.LoginService;
import com.shop.base.BaseApiService;
import com.shop.base.BaseResponse;
import com.shop.core.utils.RedisUtil;
import com.shop.feign.FeignService;
import com.shop.mapper.UserMapper;
import com.shop.mapper.entity.User;
import com.shop.member.inputDTO.UserLoginInputDTO;
import com.shop.member.inputDTO.UserRegisterDTO;
import com.shop.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * User: 王瑞文
 * Date: 2020/9/7
 * Time: 10:38
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@RestController
public class LoginServiceImpl extends BaseApiService<JSONObject> implements LoginService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private FeignService feignService;

    @Override
    public BaseResponse<JSONObject> userLogin(UserLoginInputDTO userLoginInputDTO) {
        if (userLoginInputDTO==null) return setResultError("参数接收错误！");
        String phone=userLoginInputDTO.getPhone();
        String code=userLoginInputDTO.getCode();
        String type=userLoginInputDTO.getType();
        if (type.equals("0")){ //短信
            String redisCode = redisUtil.getString(phone);
            if (StringUtils.isEmpty(redisCode)) return setResultError("验证码可能已过期！");
            User user = userService.selectByPhone(phone);
            if (user==null)return setResultError("无用户信息！");
            if (code.equals(redisCode)){
                JSONObject data = new JSONObject();
                data.put("token", userService.CreatToken(user,userLoginInputDTO.getLoginType()));
                return setResultSuccess(data);
            }
            return setResultError("验证码错误！");
        }else {//密码
            User user = userService.userLogin(phone,code);
            if (user!=null){
                JSONObject data = new JSONObject();
                data.put("token", userService.CreatToken(user,userLoginInputDTO.getLoginType()));
                return setResultSuccess(data);
            }else
                return setResultError("用户名或密码错误");
        }
    }

    @Override
    public BaseResponse<JSONObject> sendSmd(String phone) {
        User user = userService.selectByPhone(phone);
        if (user!=null){
            return feignService.sendSms(phone);
        }
        return setResultError("该账号未注册！");
    }

    @Override
    public BaseResponse<JSONObject> register(UserRegisterDTO userRegisterDTO) {
        if (userRegisterDTO==null)return setResultError("参数接受错误！");
        User user=new User();
        BeanUtils.copyProperties(userRegisterDTO,user);
       return toDaoResult(userMapper.insert(user))?setResultSuccess("注册成功！"):setResultError("注册失败！");
    }

    @Override
    public BaseResponse<JSONObject> registerSendSms(String phone) {
        return feignService.sendSms(phone);
    }
}
